import abc
from typing import List, Any, override


class ExportStrategy(metaclass=abc.ABCMeta):

	@abc.abstractmethod
	def Export(self, cfgReader) -> None:
		pass

	@abc.abstractmethod
	def ParseValue(self, typeName: str, value: Any) -> Any:
		pass

class PyDictExportStrategy(ExportStrategy):
	"""Python字典导出策略"""

	@override
	def Export(self, cfgReader) -> None:
		if not cfgReader:
			print("cfgReader is None, can't export.")
			return
		
		lstResItemStr = []
		for oItem in cfgReader:
			lstFieldKWStr = []
			for itemIdx, keyValuePair in enumerate(oItem):
				resValue = self.ParseValue(cfgReader.GetTypeNameWithIdx(itemIdx), keyValuePair.value)
				itemKwStr = "\t\t\"{}\": {}".format(keyValuePair.fieldName, resValue)
				lstFieldKWStr.append(itemKwStr)
			resFieldJoinStr = ",\n".join(lstFieldKWStr)
			lstResItemStr.append("\t{{\n{}\n\t}}".format(resFieldJoinStr))
		
		resItemJoinStr = ",\n".join(lstResItemStr)
		resCfgStr = "CFG = {{\n{}\n}}\n".format(resItemJoinStr)
		return resCfgStr


	@override
	def ParseValue(self, typeName: str, value: str) -> str:
		match typeName:
			case "int" | "long" | "long long":
				if "." in value:
					value = value.split(".")[0]
			case "float" | "double":
				if "." not in value:
					value = "{}.0".format(value)
			case "bool":
				if value == "false" or value == "0" or value == "":
					value = "False"
				elif value == "True" or value == "1":
					value = "True"
				else:
					print(f"Warning: {typeName} type filed set \"{value}\", must be \"true\" or \"false\"")
					value = "True"
			case "string":
				value = "\"{}\"".format(value)

		return value
	